package com.yimi.policyservice.service.impl;

import com.github.pagehelper.Page;
import com.yimi.policyservice.common.expetion.ApiException;
import com.yimi.policyservice.dao.policy.PolicyComminsureProductMapper;
import com.yimi.policyservice.dto.policycomminsure.PolicyComminsureBean;
import com.yimi.policyservice.dto.policycomminsure.PolicyComminsureItemDto;
import com.yimi.policyservice.dto.policycomminsure.PolicyComminsureItemPriceDto;
import com.yimi.policyservice.dto.policycomminsure.PolicyComminsureProductDto;
import com.yimi.policyservice.entity.base.policy.PolicyComminsureItem;
import com.yimi.policyservice.entity.base.policy.PolicyComminsureItemPrice;
import com.yimi.policyservice.entity.base.policy.PolicyComminsureProduct;
import com.yimi.policyservice.entity.base.system.SysUser;
import com.yimi.policyservice.param.product.ComminsureParam;
import com.yimi.policyservice.repository.PolicyComminsureItemPriceRepository;
import com.yimi.policyservice.repository.PolicyComminsureItemRepository;
import com.yimi.policyservice.repository.PolicyComminsureProductRepository;
import com.yimi.policyservice.service.PolicyComminsureService;
import com.yimi.policyservice.utils.BeanHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author jack
 */
@Service
public class PolicyComminsureServiceImpl implements PolicyComminsureService {
    @Autowired
    private PolicyComminsureProductRepository policyComminsureProductRepository ;
    @Autowired
    private PolicyComminsureItemRepository policyComminsureItemRepository ;
    @Autowired
    private PolicyComminsureItemPriceRepository policyComminsureItemPriceRepository ;
    @Autowired
    private PolicyComminsureProductMapper policyComminsureProductMapper ;


    @Override
    public PolicyComminsureProduct create(PolicyComminsureBean bean, SysUser user) {

        PolicyComminsureProduct product1 = policyComminsureProductRepository.findFirstByProductCode(bean.getProductDto().getProductCode()) ;
        if(product1!=null && !product1.getId().equals(bean.getProductDto().getId())){
            throw  new ApiException(400,"方案编号已存在");
        }
        PolicyComminsureProduct product = new PolicyComminsureProduct();
        BeanUtils.copyProperties(bean.getProductDto(),product);
        product.setCreateId(user.getId());
        product.setCreateName(user.getName());
        product.setCreateTime(new Date());
        product.setUpdateId(user.getId());
        product.setUpdateName(user.getId());
        product.setUpdateTime(new Date());
        policyComminsureProductRepository.save(product);
        List<PolicyComminsureItem> items = BeanHelper.convertList(bean.getItems(), PolicyComminsureItemDto.class,PolicyComminsureItem.class);
        items.forEach(item->{
            item.setCreateId(user.getId());
            item.setCreateTime(new Date());
            item.setUpdateId(user.getId());
            item.setUpdateTime(new Date());
            item.setProductId(product.getId());
        });
        policyComminsureItemRepository.saveAll(items);
        return product;
    }

    @Override
    public void update(PolicyComminsureBean bean, String id, SysUser user) {

    }

    @Override
    public Page<PolicyComminsureProductDto> search(ComminsureParam comminsureParam) {
        return policyComminsureProductMapper.search(comminsureParam);
    }

    @Override
    public List<PolicyComminsureProductDto> getAll() {
         return BeanHelper.convertList(policyComminsureProductRepository.findAll()
                ,PolicyComminsureProduct.class,PolicyComminsureProductDto.class) ;
    }

    @Override
    public  List<PolicyComminsureProductDto>  getAllByPermission(String userId){
        return BeanHelper.convertList(policyComminsureProductRepository.findAllByPermission(userId)
                ,PolicyComminsureProduct.class,PolicyComminsureProductDto.class) ;
    }
    @Override
    public void delete(List<String> ids) {

        policyComminsureProductRepository.deleteByIdIn(ids);
        policyComminsureItemRepository.deleteByProdictIdIn(ids);
        policyComminsureItemPriceRepository.deleteByProductId(ids);
    }

    @Override
    public PolicyComminsureBean findOne(String id) {
        PolicyComminsureProduct product = policyComminsureProductRepository.findById(id).get();
        PolicyComminsureBean res = new PolicyComminsureBean();
        PolicyComminsureProductDto productDto = new PolicyComminsureProductDto();
        BeanHelper.copyProperties(product,productDto);
        res.setProductDto(productDto);
        List<PolicyComminsureItem> items =policyComminsureItemRepository.findByProductId(id);
        res.setItems(BeanHelper.convertList(items,PolicyComminsureItem.class,PolicyComminsureItemDto.class));
        return res;
    }


    @Override
    public List<PolicyComminsureItemDto> findByproductId(String id) {
        List<PolicyComminsureItem> items =policyComminsureItemRepository.findByProductId(id);
        return  BeanHelper.convertList(items,PolicyComminsureItem.class,PolicyComminsureItemDto.class);

    }

    @Override
    public void setProice(List<PolicyComminsureItemPriceDto> bean,String productId) {
        List<String> ids = new ArrayList<>();
        ids.add(productId);
        policyComminsureItemPriceRepository.deleteByProductId(ids);
        List<PolicyComminsureItemPrice> prices =  BeanHelper.convertList(bean,PolicyComminsureItemPriceDto.class, PolicyComminsureItemPrice.class);
        policyComminsureItemPriceRepository.saveAll(prices);
    }

    @Override
    public List<PolicyComminsureItemPriceDto> getProice(String id) {
        List<PolicyComminsureItemPrice> prices =  policyComminsureItemPriceRepository.findByProductId(id);
        return BeanHelper.convertList(prices,PolicyComminsureItemPrice.class,PolicyComminsureItemPriceDto.class);
    }
}
